﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Controls
{
    public partial class QuickSearch : System.Web.UI.UserControl
    {
        public static string vendors;
        public static int priceFrom;
        public static int priceTo;
        public static string price;
        public static int selected = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // phan nay con chua on
                 int catID = -1 ;
                DataTable dt=null;
                if(Request.QueryString["catID"]!=null)
                    catID = Convert.ToInt32(Request.QueryString["catID"].ToString());
                if (catID != -1)
                {
                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_Vendor", catID, "selectvendorproduct");

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListItem lt = new ListItem(dt.Rows[i]["Vendor_Name"].ToString(), dt.Rows[i]["Vendor_ID"].ToString());
                        cbkList.Items.Add(lt);
                    }
                    cbkList.SelectedIndex = selected;
                }
            }
        }

        protected void cbkList_SelectedIndexChanged(object sender, EventArgs e)
        {
             vendors = "";
            if(selected>=0)
             cbkList.Items[selected].Selected = false;
            for (int i = 0; i < cbkList.Items.Count; i++)
            {
                if (cbkList.Items[i].Selected == true)
                    vendors += cbkList.Items[i].Value +",";
            }
            
            vendors += "null";
            selected = cbkList.SelectedIndex;

            string link = Request.Url.AbsolutePath;
            string url = Request.Url.Query;
            url = url.Replace("?", "/");
            url = url.Replace("=", "/");
            url = url.Replace("&", "/");
            url = url.Replace("currentPage", "page");


            //DataTable dt = new DataTable();
            if (link == "/ListProduct.aspx")
            {

                url += "/danhsachsanpham.htm";
            }
            if (link == "/ListBoxProduct.aspx")
            {

                url += "/boxsanpham.htm";
            }
            if (link == "/ProductSearch.aspx")
            {

                url += "/timkiem.htm";
            }
            Response.Redirect(url);

        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            // Tim kiem theo khoang gia


            priceFrom = Convert.ToInt32(Request["txtFrom"].ToString());
            priceTo = Convert.ToInt32(Request["txtTo"].ToString());

            if (priceFrom < priceTo)
            {
                price = "Product_SalesPrice > " + priceFrom + " AND Product_SalesPrice < " + priceTo;

                string link = Request.Url.AbsolutePath;
                string url = Request.Url.Query;
                url = url.Replace("?", "/");
                url = url.Replace("=", "/");
                url = url.Replace("&", "/");
                url = url.Replace("currentPage", "page");


                //DataTable dt = new DataTable();
                if (link == "/ListProduct.aspx")
                {

                    url += "/danhsachsanpham.htm";
                }
                if (link == "/ListBoxProduct.aspx")
                {

                    url += "/boxsanpham.htm";
                }
                if (link == "/ProductSearch.aspx")
                {

                    url += "/timkiem.htm";
                }
                Response.Redirect(url);
            }
        }
    }
}